﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace BattleCity.Rendering
{
    public class BackgroundRenderer
    {
        GraphicsDevice graphicsDevice;
        Texture2D background;

        VertexBuffer vertexBuffer;

        BasicEffect fx;
      
        public BackgroundRenderer(ContentManager content, GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;
            #if WINDOWS_PHONE
            background = content.Load<Texture2D>(@"background_wp7");
            #else
            background = content.Load<Texture2D>(@"background");
            #endif

            vertexBuffer = new VertexBuffer(graphicsDevice, VertexPositionTexture.VertexDeclaration, 6, BufferUsage.None);

            VertexPositionTexture[] vertexData = new VertexPositionTexture[6];
            vertexData[0] = new VertexPositionTexture(new Vector3(-70, -40, -40), new Vector2(0, 1));
            vertexData[1] = new VertexPositionTexture(new Vector3(-70, +40, -40), new Vector2(0, 0));
            vertexData[2] = new VertexPositionTexture(new Vector3(70, +40, -40), new Vector2(1, 0));
            vertexData[3] = new VertexPositionTexture(new Vector3(70, +40, -40), new Vector2(1, 0));
            vertexData[4] = new VertexPositionTexture(new Vector3(70, -40, -40), new Vector2(1, 1));
            vertexData[5] = new VertexPositionTexture(new Vector3(-70, -40, -40), new Vector2(0, 1));

            vertexBuffer.SetData<VertexPositionTexture>(vertexData);

            fx = new BasicEffect(graphicsDevice);
            fx.LightingEnabled = false;
            fx.TextureEnabled = true;
            fx.VertexColorEnabled = false;
            fx.Texture = background;
            fx.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -40), Vector3.UnitY);
            #if WINDOWS_PHONE
            fx.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, graphicsDevice.Viewport.AspectRatio, 1, 100);
            #else
            fx.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, graphicsDevice.Viewport.AspectRatio, 1, 100);
            #endif
       
        }

        public void Draw()
        {
            fx.CurrentTechnique.Passes[0].Apply();
            graphicsDevice.SetVertexBuffer(vertexBuffer);
            graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
        }


    }
}
